package com.ai.util;

import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class GetCameraPreviewURLUtil {
    public static String getCameraRtspUrl(String cameraIndexCode){
        String urlJson = GetCameraPreviewURLUtil.GetCameraPreviewURL(cameraIndexCode);
        JSONObject jsonObject = JSONObject.parseObject(urlJson);
        JSONObject msg = (JSONObject) jsonObject.get("data");
        String rtspUrl = (String) msg.get("url");
        return rtspUrl;
    }
    /**
     * 获取摄像头取流地址
     * @param cameraIndexCode 摄像头唯一标识
     * @return 取流地址
     */
    private static String GetCameraPreviewURL(String cameraIndexCode) {

        /**
         * STEP1：设置平台参数，根据实际情况,
         * 设置host appkey appsecret 三个参数.
         * 摄像头ip：port
         */
        ArtemisConfig.host = "10.168.192.31:443"; // 平台的ip端口
        ArtemisConfig.appKey = "28292616";  // 密钥appkey
        ArtemisConfig.appSecret = "gokGfaXduVFNHPvEAg6C";// 密钥appSecret

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */
//        final String previewURLsApi = ARTEMIS_PATH + "/api/video/v2/cameras/previewURLs";

        final String previewURLsApi = ARTEMIS_PATH + "/api/vnsc/mls/v1/preview/openApi/getPreviewParam";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                log.info("=========================== 访问现场地址  ===========================");
                log.info("=========================== https://" + previewURLsApi +" ===========================");
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("indexCode", cameraIndexCode);

        jsonBody.put("netZoneCode", "0");
        jsonBody.put("transmode", 1);
        //修改为子码流地址
        jsonBody.put("streamType", 1);
        jsonBody.put("protocol", "rtsp");
        jsonBody.put("expireTime", -1);
        jsonBody.put("expand", "streamform=rtp");
        String body = jsonBody.toJSONString();
        /**
         * STEP6：调用接口
         */
        log.info("===========================  调用摄像头平台入参：{}  ===========================",body);
        // post请求application/json类型参数
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);
        return result;
    }


//    public static void main(String[] args) {
//        String json ="{\"code\":\"0\",\"msg\":\"success\",\"data\":{\"url\":\"rtsp://10.168.192.38:554/openUrl/GtXyT8Q\"}}";
//
//        JSONObject jsonObject = JSONObject.parseObject(json);
//        Object msg1 = jsonObject.get("data");
//        System.out.println(msg1.toString());
//        JSONObject msg = (JSONObject) jsonObject.get("data");
//        String rtspUrl = (String) msg.get("url");
//        System.out.println(rtspUrl);
//    }
}